#include<stdio.h>

void fk(int* x,int i,int l,int t,int* mo,int*rp,int* times,int* tx,int f,int g){
	register int n = 0, m = 0;
	int xt = 0,a = 0,b = 0;
	for(n = i;n <= (t - l);++n){
		if(l == 1){
			a = mo[n];
			b = rp[n];
			xt = times[n];
			for(m = 0;m < i;++m){
				if(x[m] != 0){
					if(a + mo[m] <= g && b + rp[m] <= f){
						xt += times[m];
						a += mo[m];
						b += rp[m];
					}else{
						xt = -1;
						break;
					}
				}
			}
			*tx = (xt!= -1 && (*tx > xt || *tx == 0))?xt:*tx;
		}else{
			x[n] = 1;
			fk(x,n+1,l-1,t,mo,rp,times,tx,f,g);
			x[n] = 0;
		}
	}
}

int fk1(int* x,int len,int* mo,int*rp,int* times,int qx,int w){
	unsigned int i = 0,u = 0,q = 0,n = 0,mt = 0,rt = 0,nx = 0,tx = 0;
	unsigned int count = pow(2,len) - 1;
	for(i = count;i > 0;--i){
		n = 0;
		q = 0;
		mt = 0;
		rt = 0;
		for(u = 0;u < len;++u){
			if((i >> u) & 1){
				++n;
				q += times[u];
				mt += mo[u];
				rt += rp[u];
			}
		}
		if(mt <= qx && rt <= w){
			if(nx == n){
				if(q < tx){
					nx = n;
					tx = q;
				}
			}else if(nx < n){
					nx = n;
					tx = q;
			}
		}
	}
	return tx;
}

int main(){
	int m = 0,n = 0,i = 0,q = 0,t = 0,a = 0,b = 0,c = 0,f = 0,g = 0;
	int* times = NULL;
	int* mo = NULL;
	int* rp = NULL;
	int* x = NULL;
	FILE *fin  = fopen ("gf.in", "r");
	FILE *fout = fopen ("gf.out", "w");
	fscanf(fin,"%d\n",&i);
	times = (int*)malloc(sizeof(int) * i);
	mo = (int*)malloc(sizeof(int) * i);
	rp = (int*)malloc(sizeof(int) * i);
	x = (int*)malloc(sizeof(int) * i);
	memset(x,0,sizeof(int) * i);
	q = i;
	while(q){
		fscanf(fin,"%d %d %d\n",&m,&n,&t);
		times[i - q] = t;
		mo[i - q] = m;
		rp[i - q] = n;
		--q;
	}
	fscanf(fin,"%d %d\n",&f,&g);
	/*for(m = i;m > 0;--m){
		n = 0;
		fk(x,0,m,i,mo,rp,times,&n,f,g);
		if(n){
			break;
		}
	}*/
	n = fk1(x,i,mo,rp,times,f,g);
	free(times);
	free(c);
	fprintf(fout,"%d",n);
	fclose(fin);
	fclose(fout);
	return 0;
}
